package com.ruyuan.rapid.core.helper;

import com.ruyuan.rapid.common.enums.ResponseCode;
import com.ruyuan.rapid.core.context.RapidResponse;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;

/**
 * 响应的辅助类
 */
public class ResponseHelper {


    public static FullHttpResponse getHttpResponse(ResponseCode responseCode){
        RapidResponse rapidResponse = RapidResponse.buildRapidResponse(responseCode);
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.INTERNAL_SERVER_ERROR,
                Unpooled.wrappedBuffer(rapidResponse.getContent().getBytes()));
        response.headers().set(HttpHeaderNames.CONTENT_TYPE,HttpHeaderValues.APPLICATION_JSON + ";charset=utf-8");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH,response.content().readableBytes());
        return response;
    }

}
